#!/bin/bash

# ==================================================================
#  Stable Video Diffusion (SVD) 离线部署脚本
#  完全不依赖网络，使用本地模型文件
# ==================================================================

# 如果任何命令失败，则立即退出
set -e

echo ""
echo "=================================================="
echo "  SVD 离线部署流程"
echo "=================================================="
echo ""

# --- 检查虚拟环境 ---
if [ ! -d "svd_env" ]; then
    echo "[INFO] 创建虚拟环境..."
    python3 -m venv svd_env
fi

echo "[INFO] 激活虚拟环境..."
. svd_env/bin/activate

# --- 步骤 1: 环境配置和依赖安装 ---
echo ""
echo "[STEP 1/3] 配置环境并安装依赖包..."
python setup_environment.py
if [ $? -ne 0 ]; then
    echo "[ERROR] 环境配置失败"
    exit 1
fi
echo "[SUCCESS] 环境配置成功！"

# --- 步骤 2: 检查/准备模型 ---
echo ""
echo "[STEP 2/3] 检查模型文件..."
python offline_model_manager.py
if [ $? -ne 0 ]; then
    echo ""
    echo "=================================================="
    echo "  模型文件未准备好"
    echo "=================================================="
    echo ""
    echo "请按照上述说明准备模型文件，然后重新运行此脚本"
    echo ""
    exit 1
fi
echo "[SUCCESS] 模型文件已准备好！"

# --- 步骤 3: 启动服务 ---
echo ""
echo "[STEP 3/3] 启动离线推理服务..."
echo ""
echo "###############################################################"
echo ""
echo "  离线服务正在启动..."
echo "  访问地址: http://<your-server-ip>:8080"
echo ""
echo "  健康检查: http://<your-server-ip>:8080/health"
echo "  模型信息: http://<your-server-ip>:8080/model_info"
echo ""
echo "  按 CTRL+C 停止服务"
echo ""
echo "###############################################################"
echo ""

# 使用离线服务器脚本
python svd_server_offline.py

echo ""
echo "[INFO] 脚本执行完毕。"
